package com.cn.itcast.gjp.controller;

import com.cn.itcast.gjp.domain.Ledger;
import com.cn.itcast.gjp.domain.Sort;
import com.cn.itcast.gjp.mapper.LedgerMapper;
import com.cn.itcast.gjp.mapper.SortMapper;
import com.cn.itcast.gjp.service.SortService;
import com.cn.itcast.gjp.view.AbstractSortMngDialog;
import org.apache.ibatis.session.SqlSession;

import javax.swing.*;
import java.util.List;


public class SortMngController extends AbstractSortMngDialog {
    private SortService sortService = new SortService();

    public SortMngController(JFrame frame) {
        super(frame);
        super.setTableModel(queryAllSort());
    }


    //添加操作逻辑
    @Override
    public void addSort() {
        AddSortController addSortController = new AddSortController(this);
        addSortController.setVisible(true);
        System.out.println("SortMngController addSort  调用了");

        this.setTableModel(queryAllSort());
    }

    @Override
    public void editSort() {
        //获取用户选择的表格行号
        int row = this.sortDataTable.getSelectedRow();
        //判断行号是否小于0，如果小于0说明用户没有选择；
        if (row < 0) {
            JOptionPane.showMessageDialog(this, "请选择要编辑的分类！");
            return;
        }
        //通过表格行号获取表格中该行数据封装到Sort对象中
        Sort sort = this.getSortByTableRow(row) ;
        //判断获取的Sort对象是否为null，如果为null说明用户选择的是空行；
        if (sort == null) {
            JOptionPane.showMessageDialog(this, "您选择的是空行");
            return;
        }
        //将值赋予dialog
        new EditSortController(this, sort).setVisible(true);

        this.setTableModel(queryAllSort());
    }

    @Override
    public void deleteSort() {
        int row = this.sortDataTable.getSelectedRow();

        //检测是否已经选择
        if (row < 0){
            JOptionPane.showMessageDialog(this, "请选择要删除的分类！");
            return;
        }
        //获取用户选择的分类
        Sort sort = this.getSortByTableRow(row);
        if (sort == null){
            JOptionPane.showMessageDialog(this, "您选择的是空行");
            return;
        }

        //判断再legder中是否存在该分类

        SqlSession session= MyBatisUtil.getSqlSession();
        LedgerMapper ledgerMapper = session.getMapper(LedgerMapper.class);
        List<Ledger> ledgerBySid = ledgerMapper.queryLedgerBySid(sort.getSid());
        if (ledgerBySid.size() > 0){
            JOptionPane.showMessageDialog(this, "该分类下有账单，不能删除！");
            return;
        }

        //开启删除操作

        SortMapper sortMapper = session.getMapper(SortMapper.class);

        int result = JOptionPane.showConfirmDialog(this, "您确定要删除分类吗？");
        if (result == 0) {
            System.out.println("删除分类");
            JOptionPane.showMessageDialog(this, "删除成功！");
            sortMapper.DeleteSort(sort);

            session.commit();
        }
        session.close();
        System.out.println("删除分类");
        this.setTableModel(queryAllSort());
    }

    @Override
    public List<Sort> queryAllSort() {
        SqlSession session= MyBatisUtil.getSqlSession();
        SortMapper sortMapper = session.getMapper(SortMapper.class);
        return sortMapper.queryAllSort();
    }
}
